#include "LocalOrderLoader.h"

LocalOrderLoader::LocalOrderLoader(const QString &user, const QString &passwd, uint bufferCapacity)
    : BaseLoader(user, passwd, bufferCapacity)
{
    if (!_sqlQuery.exec("use dblocalorder;") || !_sqlQuery.exec("CALL cleanDatabase;")) {
        cout << "Nao foi possivel limpar o conteudo do banco de dados" << endl;
        cout << _sqlQuery.lastError().text().toStdString() << endl;
        throw -1;
    }
}

EdgeElement* LocalOrderLoader::newElement(EdgeElement* parent, uint id, const QString& value, uint path_id, bool attribute) const
{
    EdgeElement * element = new EdgeElement();
    // incrementa antes de adicionar, pois a raiz deve ter código 1
    // código 0 fica como "pai da raiz"
    element->id = id;
    element->path_id = path_id;
    element->end_desc_id = element->id;
    if (parent) {
        element->parent_id = parent->id;
        element->order = parent->num_children++;
//        parent->end_desc_id = element->id;
    } else {
        element->parent_id = 0;
        element->order = 0;
    }
    element->num_children = 0;
    element->value = value;
    element->attribute = attribute;

    return element;
}
